package com.xiaokaceng.openci.dto;

import java.util.HashSet;
import java.util.Set;

import com.xiaokaceng.openci.domain.Developer;
import com.xiaokaceng.openci.domain.Project;
import com.xiaokaceng.openci.domain.ProjectDeveloper;
import com.xiaokaceng.openci.domain.Tool;

public class ProjectDetailDto extends BaseDto {

	private static final long serialVersionUID = 891949416201125917L;

	private Long id;

	private String name;

	private String projectStatus;

	private String projectCreateDate;

	private String artifactId;

	private String groupId;

	private Set<ProjectDeveloperDto> projectDeveloperDtos;

	private Set<ToolDto> toolDtos;
	
	public static ProjectDetailDto transformBy(Project project) {
		ProjectDetailDto projectDetailDto = new ProjectDetailDto();
		projectDetailDto.setId(project.getId());
		projectDetailDto.setName(project.getName());
		projectDetailDto.setGroupId(project.getProjectDetail().getGroupId());
		projectDetailDto.setArtifactId(project.getProjectDetail().getArtifactId());
		projectDetailDto.setProjectCreateDate(dateToString(project.getProjectCreateDate()));
		projectDetailDto.setProjectStatus(project.getProjectStatus().toString());
		projectDetailDto.setProjectDeveloperDtos(createProjectDeveloperDtos(project.getDevelopers()));
		projectDetailDto.setToolDtos(createToolDtos(project.getTools()));
		return projectDetailDto;
	}

	private static Set<ToolDto> createToolDtos(Set<Tool> tools) {
		Set<ToolDto> toolDtos = new HashSet<ToolDto>();
		for (Tool each : tools) {
			ToolDto toolDto = new ToolDto();
			toolDto.setToolIntegrationStatus(each.getStatus().toString());
			toolDto.setIntegrationDate(dateToString(each.getIntegrationDate()));
			toolDto.setRequestAddress(each.getToolRequestAddress());
			toolDto.setToolConfigurationDto(ToolConfigurationDto.transformBy(each.getToolConfiguration()));
			toolDto.setToolInterfaceImplementDtos(ToolInterfaceImplementDto.transformBy(each.getToolInterfaceImplements()));
			toolDtos.add(toolDto);
		}
		return toolDtos;
	}

	private static Set<ProjectDeveloperDto> createProjectDeveloperDtos(Set<ProjectDeveloper> developers) {
		Set<ProjectDeveloperDto> projectDeveloperDtos = new HashSet<ProjectDeveloperDto>();
		for (ProjectDeveloper each : developers) {
			ProjectDeveloperDto projectDeveloperDto = new ProjectDeveloperDto();
			Developer developer = each.getDeveloper();
			projectDeveloperDto.setDeveloperId(developer.getId());
			projectDeveloperDto.setName(developer.getName());
			projectDeveloperDto.setEmail(developer.getEmail());
			projectDeveloperDto.setNotice(each.isNotice());
			projectDeveloperDto.setSyncCas(each.isSyncCas());
			projectDeveloperDto.setNoticeFailureCause(each.getNoticeFailureCause());
			projectDeveloperDto.setSyncCasFailureCause(each.getSyncCasFailureCause());
			projectDeveloperDtos.add(projectDeveloperDto);
		}
		return projectDeveloperDtos;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProjectStatus() {
		return projectStatus;
	}

	public void setProjectStatus(String projectStatus) {
		this.projectStatus = projectStatus;
	}

	public String getProjectCreateDate() {
		return projectCreateDate;
	}

	public void setProjectCreateDate(String projectCreateDate) {
		this.projectCreateDate = projectCreateDate;
	}

	public String getArtifactId() {
		return artifactId;
	}

	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public Set<ProjectDeveloperDto> getProjectDeveloperDtos() {
		return projectDeveloperDtos;
	}

	public void setProjectDeveloperDtos(Set<ProjectDeveloperDto> projectDeveloperDtos) {
		this.projectDeveloperDtos = projectDeveloperDtos;
	}

	public Set<ToolDto> getToolDtos() {
		return toolDtos;
	}

	public void setToolDtos(Set<ToolDto> toolDtos) {
		this.toolDtos = toolDtos;
	}

}
